package com.approval.approvalapp.index.cygn.xxbs;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.MoniterBean;
import com.approval.approvalapp.bean.UpSuccessBean;
import com.approval.approvalapp.bean.XxbslmBean;
import com.approval.approvalapp.index.cygn.qjshenqing.adapter.PhotoRvAdapter;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.PicPreviewActivity;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.ToastUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.base.ContantActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.net.interfacepac.NetUpLoadResponse;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.zzti.fengyongge.imagepicker.PhotoSelectorActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//信息报送
public class XxbsActivity extends BaseActivity implements PhotoRvAdapter.OnRecyclerViewItemClickListener {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.bt_tv)
    TextView btTv;
    @Bind(R.id.bt_ll)
    LinearLayout btLl;
    @Bind(R.id.sslm_tv)
    TextView sslmTv;
    @Bind(R.id.sslm_ll)
    LinearLayout sslmLl;
    @Bind(R.id.ly_tv)
    TextView lyTv;
    @Bind(R.id.qtzgr_tv)
    TextView qtzgrTv;
    @Bind(R.id.qtzgr_ll)
    LinearLayout qtzgrLl;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Bind(R.id.nr)
    TextView nr;
    @Bind(R.id.commit)
    TextView commit;
    @Bind(R.id.dyzgr_rv)
    TextView dyzgr_rv;
    @Bind(R.id.nrll)
    LinearLayout nrll;
    @Bind(R.id.zw_tv)
    TextView zwTv;
    @Bind(R.id.zw_ll)
    LinearLayout zwLl;
    @Bind(R.id.xm_tv)
    TextView xmTv;
    @Bind(R.id.xm_ll)
    LinearLayout xmLl;
    private ArrayList<String> photos = new ArrayList<>();
    ArrayList<String> localUrl = new ArrayList<>();
    private PhotoRvAdapter adapter;
    //标题字符串
    private String btStr;
    //其他撰稿人
    private String qtzgrStr;
    //内容
    private String nrStr;
    //职务
    private String zwStr;
    //姓名
    private String xmStr;
    private List<XxbslmBean> xxbslmBeanList = new ArrayList<>();
    private List<MoniterBean> moniterBeanList = new ArrayList<>();
    private String tid;

    @Override
    public int getLayoutId() {
        return R.layout.activity_xxbs;
    }

    @Override
    public void initView() {
        lyTv.setText(SpUtil.getString(this, SpUtil.UNIT_NAME, ""));
        dyzgr_rv.setText(SpUtil.getString(this, SpUtil.USER_RUN_NICKNAME, ""));
        adapter = new PhotoRvAdapter(this, localUrl, 1);
        rv.setLayoutManager(new GridLayoutManager(this, 1));
        rv.setHasFixedSize(true);
        rv.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        NetRequsetUtil.getInstance().netRequestGet("v1/center/submitted_cat", new NetDownResponse() {
            @Override
            public void success(String str) {
                xxbslmBeanList = JSON.parseArray(str, XxbslmBean.class);
            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {

            }
        });
    }


    @OnClick({R.id.back_iv, R.id.bt_ll, R.id.sslm_ll, R.id.qtzgr_ll, R.id.commit, R.id.nrll
            , R.id.zw_ll, R.id.xm_ll})
    public void onViewClicked(View view) {
        final JSONObject jsonObject = new JSONObject();
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.bt_ll:
                ContantActivity.goInto(this, 1, "标题", btStr);
                break;
            case R.id.sslm_ll:
                if (xxbslmBeanList == null || xxbslmBeanList.size() == 0) {
                    return;
                }
                for (int i = 0; i < xxbslmBeanList.size(); i++) {
                    moniterBeanList.add(new MoniterBean(xxbslmBeanList.get(i).getId(), xxbslmBeanList.get(i).getTitle()));
                }
                OptionsPickerView pvOptions1 = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        String tx = moniterBeanList.get(options1).getName();
                        tid = moniterBeanList.get(options1).getId();
                        sslmTv.setText(tx);
                        if (tx.equals("领导视察")) {
                            zwLl.setVisibility(View.VISIBLE);
                            xmLl.setVisibility(View.VISIBLE);
                        } else {
                            zwLl.setVisibility(View.GONE);
                            xmLl.setVisibility(View.GONE);
                        }
                    }
                }).build();
                pvOptions1.setPicker(moniterBeanList, null, null);
                pvOptions1.show();
                break;
            case R.id.qtzgr_ll:
                ContantActivity.goInto(this, 2, "其他撰稿人", qtzgrStr);
                break;
            case R.id.nrll:
                ContantActivity.goInto(this, 3, "内容", nrStr);
                break;
            case R.id.zw_ll:
                ContantActivity.goInto(this, 4, "职务", zwStr);
                break;
            case R.id.xm_ll:
                ContantActivity.goInto(this, 5, "姓名", xmStr);
                break;
            case R.id.commit:


                if (localUrl.size() == 0){
                //没附件
                    if (sslmTv.getText().toString().equals("请选择")) {
                        Toast.makeText(this, "请选择所属栏目", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (sslmTv.getText().toString().equals("领导视察")) {
                        if (TextUtils.isEmpty(btStr) ||
                                TextUtils.isEmpty(nrStr) ||
                                TextUtils.isEmpty(zwStr) ||
                                TextUtils.isEmpty(xmStr)) {
                            Toast.makeText(this, "请填写完整", Toast.LENGTH_SHORT).show();
                            return;
                        }

                        jsonObject.put("cat_id",tid);
                        jsonObject.put("region_code",SpUtil.getString(this,SpUtil.REGION_CODE,""));
                        jsonObject.put("title",btStr);
                        jsonObject.put("org_code",SpUtil.getString(this,SpUtil.ORG_CODE,""));
                        jsonObject.put("org_name",SpUtil.getString(this,SpUtil.UNIT_NAME,""));
                        jsonObject.put("author",SpUtil.getString(this, SpUtil.USER_RUN_NICKNAME, ""));
                        jsonObject.put("zw",zwStr);
                        jsonObject.put("xm",xmStr);
                        jsonObject.put("content",nrStr);
                        if (!TextUtils.isEmpty(qtzgrStr)){
                            jsonObject.put("qt_author",qtzgrStr);
                        }

                    }else {
                        if (TextUtils.isEmpty(btStr) ||
                                TextUtils.isEmpty(nrStr) ) {
                            Toast.makeText(this, "请填写完整", Toast.LENGTH_SHORT).show();
                            return;
                        }

                        jsonObject.put("cat_id",tid);
                        jsonObject.put("region_code",SpUtil.getString(this,SpUtil.REGION_CODE,""));
                        jsonObject.put("title",btStr);
                        jsonObject.put("org_code",SpUtil.getString(this,SpUtil.ORG_CODE,""));
                        jsonObject.put("org_name",SpUtil.getString(this,SpUtil.UNIT_NAME,""));
                        jsonObject.put("author",SpUtil.getString(this, SpUtil.USER_RUN_NICKNAME, ""));
                        jsonObject.put("content",nrStr);
                        if (!TextUtils.isEmpty(qtzgrStr)){
                            jsonObject.put("qt_author",qtzgrStr);
                        }
                    }


                    NetRequsetUtil.getInstance().netRequestPostJson("v1/center/submitted", jsonObject.toString(), new NetDownResponse() {
                        @Override
                        public void success(String str) {
                            ToastUtil.showToast(XxbsActivity.this, "提交成功");
                            XxbsActivity.this.finish();
                        }

                        @Override
                        public void errowithresponse(String str) {
                            ErrorBeanUtil.getErrorMessage(str);
                        }

                        @Override
                        public void erro() {

                        }

                        @Override
                        public void finish() {

                        }
                    });
                }else {
                    File file = new File(localUrl.get(0));
                    NetRequsetUtil.getInstance().uploadFile("v1/center/do_upload",
                            localUrl.get(0),
                            "file_name", file.getName(), new NetUpLoadResponse() {
                                @Override
                                public void success(String str) {
                                    UpSuccessBean upSuccessBean = JSON.parseObject(str, UpSuccessBean.class);
                                    String file_url = upSuccessBean.getFile_url();

                                    if (sslmTv.getText().toString().equals("请选择")) {
                                        Toast.makeText(XxbsActivity.this, "请选择所属栏目", Toast.LENGTH_SHORT).show();
                                        return;
                                    }
                                    if (sslmTv.getText().toString().equals("领导视察")) {
                                        if (TextUtils.isEmpty(btStr) ||
                                                TextUtils.isEmpty(nrStr) ||
                                                TextUtils.isEmpty(zwStr) ||
                                                TextUtils.isEmpty(xmStr)) {
                                            Toast.makeText(XxbsActivity.this, "请填写完整", Toast.LENGTH_SHORT).show();
                                            return;
                                        }

                                        jsonObject.put("cat_id",tid);
                                        jsonObject.put("region_code",SpUtil.getString(XxbsActivity.this,SpUtil.REGION_CODE,""));
                                        jsonObject.put("title",btStr);
                                        jsonObject.put("org_code",SpUtil.getString(XxbsActivity.this,SpUtil.ORG_CODE,""));
                                        jsonObject.put("org_name",SpUtil.getString(XxbsActivity.this,SpUtil.UNIT_NAME,""));
                                        jsonObject.put("author",SpUtil.getString(XxbsActivity.this, SpUtil.USER_RUN_NICKNAME, ""));
                                        jsonObject.put("zw",zwStr);
                                        jsonObject.put("xm",xmStr);
                                        jsonObject.put("content",nrStr);
                                        if (!TextUtils.isEmpty(qtzgrStr)){
                                            jsonObject.put("qt_author",qtzgrStr);
                                        }

                                    }else {
                                        if (TextUtils.isEmpty(btStr) ||
                                                TextUtils.isEmpty(nrStr) ) {
                                            Toast.makeText(XxbsActivity.this, "请填写完整", Toast.LENGTH_SHORT).show();
                                            return;
                                        }

                                        jsonObject.put("cat_id",tid);
                                        jsonObject.put("region_code",SpUtil.getString(XxbsActivity.this,SpUtil.REGION_CODE,""));
                                        jsonObject.put("title",btStr);
                                        jsonObject.put("org_code",SpUtil.getString(XxbsActivity.this,SpUtil.ORG_CODE,""));
                                        jsonObject.put("org_name",SpUtil.getString(XxbsActivity.this,SpUtil.UNIT_NAME,""));
                                        jsonObject.put("author",SpUtil.getString(XxbsActivity.this, SpUtil.USER_RUN_NICKNAME, ""));
                                        jsonObject.put("content",nrStr);
                                        jsonObject.put("file",file_url);
                                        if (!TextUtils.isEmpty(qtzgrStr)){
                                            jsonObject.put("qt_author",qtzgrStr);
                                        }
                                    }


                                    NetRequsetUtil.getInstance().netRequestPostJson("v1/center/submitted", jsonObject.toString(), new NetDownResponse() {
                                        @Override
                                        public void success(String str) {
                                            ToastUtil.showToast(XxbsActivity.this, "提交成功");
                                            XxbsActivity.this.finish();
                                        }

                                        @Override
                                        public void errowithresponse(String str) {
                                            ErrorBeanUtil.getErrorMessage(str);
                                        }

                                        @Override
                                        public void erro() {

                                        }

                                        @Override
                                        public void finish() {

                                        }
                                    });

                                }

                                @Override
                                public void erro() {

                                }
                            });
                }

                break;
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        switch (position) {
            case -1:
                Intent intent = new Intent(this, PhotoSelectorActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                intent.putExtra("limit", 1 - localUrl.size());//number是选择图片的数量
                startActivityForResult(intent, 0);

                break;
            default:
                PicPreviewActivity.goInto(this, localUrl.get(position));
                break;
        }
    }

    @Override
    public void onItemDeltetClick(View view, int position) {
        localUrl.remove(position);
        adapter.setImages(localUrl);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0:
                if (data != null) {
                    photos = (ArrayList<String>) data.getExtras().getSerializable("photos");//path是选择拍照或者图片的地址数组
                    if (photos != null) {
                        localUrl.addAll(photos);
                    }
                    adapter.setImages(localUrl);

                }
                break;
            case 1:
                if (data != null) {
                    btStr = data.getStringExtra(ContantActivity.returnkey);
                    btTv.setText(btStr);
                }
                break;
            case 2:
                if (data != null) {
                    qtzgrStr = data.getStringExtra(ContantActivity.returnkey);
                    qtzgrTv.setText(qtzgrStr);
                }
                break;
            case 3:
                if (data != null) {
                    nrStr = data.getStringExtra(ContantActivity.returnkey);
                    nr.setText(nrStr);
                }
                break;
            case 4:
                if (data != null) {
                    zwStr = data.getStringExtra(ContantActivity.returnkey);
                    zwTv.setText(zwStr);
                }
                break;
            case 5:
                if (data != null) {
                    xmStr = data.getStringExtra(ContantActivity.returnkey);
                    xmTv.setText(xmStr);
                }
                break;
        }
    }

}
